package com.longshihan.carservice.service.Cart;

import com.google.gson.reflect.TypeToken;
import com.longshihan.carservice.dao.CartDao;
import com.longshihan.carservice.dao.GoodsDao;
import com.longshihan.carservice.model.CartGoodsDomain;
import com.longshihan.carservice.model.OrderGoodsDomain;
import com.longshihan.carservice.utils.GsonUtils;
import org.apache.http.util.TextUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.Type;
import java.util.*;

@Service
public class CartServiceImpl implements CartService {

    @Autowired
    private CartDao cartDao;

    @Autowired
    private GoodsDao goodsDao;


    @Override
    public int updateGoods(String userId, List<OrderGoodsDomain> goods) {
        //先判断是否有
        String goodStr= GsonUtils.toJsonString(goods);
        CartGoodsDomain domain=cartDao.getCartGoods(userId);
        if (domain==null){
            return cartDao.addGoods(userId,goodStr);
        }else {
            if (!TextUtils.isBlank(domain.getGoods())){
                Map<String,OrderGoodsDomain> map=new HashMap<>();
                Type type = new TypeToken<List<OrderGoodsDomain>>(){}.getType();
                List<OrderGoodsDomain> cartBeans= (List<OrderGoodsDomain>) GsonUtils.JSONToObject(domain.getGoods(),type);
                for (int i = 0; i <cartBeans.size(); i++) {
                    map.put(cartBeans.get(i).getGoodsId(),cartBeans.get(i));
                }
                for (int i = 0; i <goods.size(); i++) {
                    map.put(goods.get(i).getGoodsId(),goods.get(i));
                }

                //过滤掉个数为0的商品
                Iterator<OrderGoodsDomain> it = map.values().iterator();
                while(it.hasNext()){
                    OrderGoodsDomain cartBean = it.next();
                    if(cartBean.getNum()<=0){
                        it.remove();
                    }
                }
               goodStr=GsonUtils.toJsonString(map.values());
            }
            return cartDao.updateGoods(userId,goodStr);


        }

    }

    @Override
    public int updateGood(String userId, OrderGoodsDomain good) {
        String goodStr;
        CartGoodsDomain domain=cartDao.getCartGoods(userId);
        if (domain==null){
            List<OrderGoodsDomain> cartBeans=new ArrayList<>();
            cartBeans.add(good);
            goodStr=GsonUtils.toJsonString(cartBeans);
            return cartDao.addGoods(userId,goodStr);
        }else {
            Type type = new TypeToken<List<OrderGoodsDomain>>(){}.getType();
            List<OrderGoodsDomain> cartBeans= (List<OrderGoodsDomain>) GsonUtils.JSONToObject(domain.getGoods(),type);
            if (good.getNum()==0){//
                Iterator<OrderGoodsDomain> it = cartBeans.iterator();
                while(it.hasNext()){
                    OrderGoodsDomain cartBean = it.next();
                    if(good.getGoodsId().equals(cartBean.getGoodsId())){
                        it.remove();
                    }
                }

            }else {
                boolean isUpdate=false;
                for (int i = 0; i <cartBeans.size(); i++) {
                    if (good.getGoodsId().equals(cartBeans.get(i).getGoodsId())){
                        cartBeans.get(i).setNum(good.getNum());
                        isUpdate=true;
                        break;
                    }
                }
                if (!isUpdate){
                    cartBeans.add(good);
                }
            }
            goodStr=GsonUtils.toJsonString(cartBeans);
        }
        return cartDao.updateGoods(userId,goodStr);
    }

    public OrderGoodsDomain getCartBean(List<OrderGoodsDomain> goods,String key){
        OrderGoodsDomain cartBean = null;
        for (int i = 0; i <goods.size(); i++) {
            if (key.equals(goods.get(i).getGoodsId())){
                cartBean=goods.get(i);
                break;
            }
        }
        return cartBean;
    }

    @Override
    public List<OrderGoodsDomain> getCartGoods(String userId) {
        CartGoodsDomain domain=cartDao.getCartGoods(userId);
        if (domain!=null&& !TextUtils.isBlank(domain.getGoods())){
            Type type = new TypeToken<List<OrderGoodsDomain>>(){}.getType();
            List<OrderGoodsDomain> cartBeans= (List<OrderGoodsDomain>) GsonUtils.JSONToObject(domain.getGoods(),type);
            //查询商品信息

            return cartBeans;
        }else {
            return new ArrayList<>();
        }
    }
}
